package com.lswms.modules.lswms.service.impl.base;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.dao.base.CodelistDao;
import com.lswms.modules.lswms.dto.base.CodelistDTO;
import com.lswms.modules.lswms.entity.base.CodeEntity;
import com.lswms.modules.lswms.entity.base.CodelistEntity;
import com.lswms.modules.lswms.service.base.CodelistService;

import lombok.extern.slf4j.Slf4j;

/**
 * 盘点
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-08-30
 */
@Service
@Slf4j
public class CodelistServiceImpl extends CrudServiceImpl<CodelistDao, CodelistEntity, CodelistDTO>
		implements CodelistService {

	@Autowired
	private CodelistDao codelistDao;

	@Override
	public QueryWrapper<CodelistEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");
		String code = (String) params.get("code");

		QueryWrapper<CodelistEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "id", id);
		wrapper.eq(StringUtils.isNotBlank(code), "code", code);
		// wrapper.like(StringUtils.isNotBlank(codename), "name", codename);

		wrapper.orderByDesc("addtime");
		return wrapper;
	}

	@Override
	public List<CodelistEntity> listByCode(Map<String, Object> params) {
		String code = (String) params.get("code");
		QueryWrapper<CodelistEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("code", code);

		wrapper.orderByAsc("sort");

		return baseDao.selectList(wrapper);
	}

	@Override
	public void deleteByCode(String[] codes) {
		QueryWrapper<CodelistEntity> wrapper = new QueryWrapper<>();
		wrapper.in(StringUtils.isNoneBlank(codes), "code", Arrays.asList(codes));
		codelistDao.delete(wrapper);

	}

	@Override
	public String validate(CodelistDTO dto) {
		String code = dto.getCode();

		if (StringUtils.isEmpty(code)) {
			return String.format(MEGConstant.IS_NOT_EMPTY, "Code");
		}

		String list = dto.getList();
		if (StringUtils.isEmpty(list)) {
			return String.format(MEGConstant.IS_NOT_EMPTY, "list");
		}

		if (StringUtils.isEmpty(dto.getListname())) {
			return String.format(MEGConstant.IS_NOT_EMPTY, "Listname");
		}

		QueryWrapper<CodelistEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("code", code);
		wrapper.eq("list", list);

		Integer count = codelistDao.selectCount(wrapper);
		if (count > 0) {
			return String.format(MEGConstant.VALUE_EXISTS, code);
		}
		return "";
	}

	@Override
	public String validateUpdate(CodelistDTO dto) {
		String code = dto.getCode();

		if (StringUtils.isEmpty(code)) {
			return String.format(MEGConstant.IS_NOT_EMPTY, "Code");
		}

		String list = dto.getList();
		if (StringUtils.isEmpty(list)) {
			return String.format(MEGConstant.IS_NOT_EMPTY, "list");
		}

		if (StringUtils.isEmpty(dto.getListname())) {
			return String.format(MEGConstant.IS_NOT_EMPTY, "Listname");
		}

		return "";
	}

}